require(['grid', 'topBar', 'layout','select2'], function () {
    var editDialog,funName, $top = $('#top'),$grid = $('#grid'),reloadGridData;

    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                commonFun.execParentMethod('getUserInfo').roleType ==='ROLT0001' ?   {
                    text: '所属企业',
                    name: 'corpId',
                    option: {
                        url: '/sysCorpInfo/getCorpTree.htm',
                        name: 'corpName',
                        idKey: 'corpId',
                        pIdKey: 'parentCorpId'
                    },
                    width: 150,
                    type: 'text-tree'
                } : '',
                {
                    text: '所属机构',
                    name: 'qOrganId',
                    option: {
                        url: '/baseOrgan/getOrganTree.htm',
                        name: 'organName',
                        idKey: 'organId',
                        pIdKey: 'parentOrganId'
                    },
                    width: 150,
                    type: 'text-tree'
                },
                {
                    text: '设备ID/车牌号',
                    name: 'qlicPltNo',
                    width: 80,
                    type: 'text'
                },
                {
                    text: '设备状态',
                    name: 'qEqmtSt',
                    width: 80,
                    type: 'select',
                    dict:'EQS00000'
                },
                {
                    text: '查询',
                    type: 'btn',
                    click: function () {
                    	reloadGridData();
                    }
                }

            ],
            right: [
                {
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                            click: function () {
                            	exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                            click: function () {
                            	exportFile('Pdf');
                            }
                        }
                    ]
                },
                {
                    text: '删除',
                    type: 'btn',
                    width: '698',
                    authority: '',
                    click: function () {
                    	deleteObj(true);
                    }
                },
                {
                    text: '+添加',
                    type: 'btn',
                    authority: '',
                    click: function () {
                    	getEditDialog('add','新增').dialog('exec', 'reset');
                    }
                }
            ]
        });
    }

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/equipmentInfo/searchEquipmentInfoPage.htm",
            colModel: [
                {name: "eqmtId",label: 'eqmtId', key: true, hidden: true},
                {name: "eqmtCd",label: '设备ID', width: 100},
                {name: "simCardno",label: 'SIM卡号',align:'right'},
                {name: "vrsnName",label: '版本名称', width: 200},
                {name: "organName",label: '所属机构', width: 150},
                {name: "licPltNo",label: '关联车辆', width: 100},
                {name: "produceFacty",label: '生产厂家', width: 150, dict: "EQFA0000"},
                {name: "prchDate", label: '购买日期', width: 100},
                {name: "eqmtSt",label: '设备状态', width: 100, dict: "EQS00000"},
                {name: "remark",label: '备注', width: 100},
                {name: "operate", width: 130, label: '操作', title: false,align:'center',
                    formatter: 'operArea',
                    formatoptions: [{
                        text: '编辑',
                        css:'operate-edit',
                        click: function (data) {
                        	 getEditDialog('edit','编辑').dialog('exec', 'setVal',data.eqmtId);
                        }
                    }, {
                        text: '删除',
                        css:'operate-delete',
                        click: function (data) {
                        	deleteObj(false,data.eqmtId);
                        }
                    }]
            
                }
            ],
            multiselect: true
        });
    }

    /**
     * 获取表单对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    function getEditDialog(flag,option) {
    	funName =flag;
        option = option || {};
        if (editDialog) {
            editDialog.dialog('open');
            if (option.title) {
                editDialog.dialog('setOption', 'title', option.title);
            }
        } else {
            editDialog = $('<div>').dialog({
                title: option.title || '设备信息维护',
                url: './master/setting/sysEquipInfoSetEdit.htm',
                height: 550,
                width: 720,
                autoOpen: true,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                        	var _t= this;
                        	//funName :  add or edit
	                       	$(_t).dialog('exec', funName,function(){
	                		    	reload(true);
                		    });
                        }
                    },
                    {
                        text: '重置',
                        css: 'btn-cancel',
                        click: function () {
                        	$(this).dialog('exec', 'reset');
                        }
                    }
                ]
            });
        }
        return editDialog;
    }

    //删除
    function deleteObj(isBatch,objId){
    	var objIds = objId;
    	if(isBatch){
    		objIds =  $grid.grid('getGridParam', 'selarrrow').toString();
	    	if(objIds == ""){
				$.messager.alert("提示", "请至少选择一条数据!");
				return false;
			}
    	}
    	$.messager.confirm('删除', '确定删除数据吗？', 'delete', function (r) {
	        if (r) {
				$.ajaxExt({
					async:true,
		    		url : "/equipmentInfo/delEquipmentInfo.htm",
					data : {'eqmtIds' : objIds}
				}).done(function(){
					//$.messager.alert("提示","删除成功。","success");
					//reloadGridData();
					reload();
				});
	        }
    	});
	}
    //查询grid
	reloadGridData = function(){
		 $grid.grid('setGridParam', {
             datatype: "JSON",
             page: 1,
             postData: $top.getValues()
         }).trigger("reloadGrid");
	};
	
	//导出
	function exportFile(suffix){
		if($grid.getGridParam("reccount")>0){
			var postData=$top.parent().getValues();
    		$.exportFile("/equipmentInfo/exportEquipmentInfo"+suffix+".htm",postData);
		}
		else{
			$.messager.alert("警告","请先查询数据！","info");
		}
    }
	
	//新增修改后执行
	function reload(close){
    	reloadGridData();
    	close && editDialog.dialog('close');
        commonFun.reloadLeftTree();
	};
	
    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();
    
    //进页面后查询
	reloadGridData();
});